package com.yyk.yiliao.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.yyk.yiliao.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ${zk} on 2018/8/15 0015.
 */

public class Detailllist_Gv_Adapter extends BaseAdapter {
    private List<String> list = new ArrayList<>();
    private Context context;
    //记录checkbox的状态
    HashMap<Integer, Boolean> state = new HashMap<Integer, Boolean>();


    public Detailllist_Gv_Adapter(Context context, List<String> list) {
        this.context = context;
        this.list = list;
    }

    public void setData(List<String> itemList) {
        if (itemList.size() > 0) {
            list = itemList;
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_flowlayout_detaillist, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.checkbox.setText(list.get(position) + "");
        holder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
//                    state.put(position, list.get(posit + "");
                } else {
                    state.remove(position);
                }
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private CheckBox checkbox;

        public ViewHolder(View item) {
            checkbox = item.findViewById(R.id.checkbox);
        }
    }
}
